<template>
  <div class="customer-tracking-tracklist-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button @click="backTrackingBusiness">返回</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          新增跟进记录
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <!-- <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane
        v-for="(item, index) in tabList"
        :key="index"
        :label="item.label"
        :name="item.value"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="跟进ID"
            prop="TrackID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="跟进人"
            prop="StaffName"
            show-overflow-tooltip
            width="100"
          />
          <el-table-column
            align="center"
            label="跟进时间"
            prop="TrackTime"
            show-overflow-tooltip
            width="180"
          />
          <el-table-column align="center" label="跟进方式" width="120">
            <template #default="{ row }">
              <span v-if="row.TrackType === 1">电话</span>
              <span v-if="row.TrackType === 2">上门</span>
              <span v-if="row.TrackType === 3">客户到公司</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="成熟度"
            prop="TrackLevel"
            show-overflow-tooltip
            width="180"
          />
          <el-table-column
            align="center"
            label="跟进内容"
            prop="TrackMemo"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="下次跟进时间"
            prop="NextTrackTime"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button text @click="handleEdit(row)">修改</el-button>
              <el-button text @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs> -->
    <el-table v-loading="listLoading" :data="list">
      <el-table-column
        align="center"
        label="跟进ID"
        prop="TrackID"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="跟进人"
        prop="StaffName"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="跟进时间"
        prop="TrackTime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column align="center" label="跟进方式" width="120">
        <template #default="{ row }">
          <span v-if="row.TrackType === 1">电话</span>
          <span v-if="row.TrackType === 2">上门</span>
          <span v-if="row.TrackType === 3">客户到公司</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="成熟度"
        prop="TrackLevel"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column align="left" label="跟进内容" width="400">
        <template #default="{ row }">
          <div v-html="row.TrackMemo"></div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="下次跟进时间"
        prop="NextTrackTime"
        show-overflow-tooltip
      />
      <!-- <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row)">修改</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/customerTrackingTracklist'
  import Edit from './components/CustomerTrackingTracklistEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerTrackingList',
    components: { Edit },
    setup() {
      const route = useRoute()
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tabList: [
          {
            label: '自己的',
            value: 'oneself',
          },
          {
            label: '他人的',
            value: 'other',
          },
        ],
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          tab: 'oneself',
          CustomerPickID: 0,
          OrderID: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const backTrackingBusiness = () => {
        router.go(-1)
        // router.push({
        //   path: '/customer/track/trackingBusiness',
        //   query: {
        //     CustomerPickID: state.queryForm.CustomerPickID,
        //   },
        // })
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.TrackID) {
          state.editRef.showEdit(row)
        } else {
          const addnew = {
            TrackID: 0,
            TrackClass: 0,
            CustomerPickID: state.queryForm.CustomerPickID,
            OrderID: state.queryForm.OrderID,
          }
          state.editRef.showEdit(addnew)
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      onMounted(() => {
        if (route.query.CustomerPickID != null && route.query.OrderID != null) {
          state.queryForm.CustomerPickID = route.query.CustomerPickID
          state.queryForm.OrderID = route.query.OrderID
        }
        fetchData()
      })

      return {
        ...toRefs(state),
        backTrackingBusiness,
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        handleTabClick,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
